package cn.com.ahao.gateway.context;

public class GatewayContextHolder {

    private static ThreadLocal<GatewayContext> context = new ThreadLocal<>();

    public static GatewayContext getContext() {
        GatewayContext gatewayContext = context.get();
        if (gatewayContext == null) {
            gatewayContext = new GatewayContext();
            context.set(gatewayContext);
        }
        return gatewayContext;
    }

    public static void setContext(GatewayContext context) {
        GatewayContextHolder.context.set(context);
    }

    public static void setStartTime(Long startTime) {
        GatewayContext gatewayContext = context.get();
        if (gatewayContext == null) {
            gatewayContext = new GatewayContext();
        }
        gatewayContext.setStartTime(startTime);
        context.set(gatewayContext);
    }

    public static Long getStartTime() {
        GatewayContext gatewayContext = context.get();
        if (gatewayContext != null) {
            return gatewayContext.getStartTime();
        }
        return null;
    }

}
